<?php
/**
 * Author: lf
 * Blog: https://blog.feehi.com
 * Email: job@feehi.com
 * Created at: 2017-08-30 19:04
 */

namespace api\models;

use Exception;
use Yii;
use common\helpers\Util;
use yii\helpers\ArrayHelper;

class User extends \common\models\User
{

    private $_personal_cer;
    private $_business_cer;


    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%user}}';
    }

    public function fields()
    {
        return [
            'id',
            'username',
            'avatar'       => function ($model) {
//                $cdn       = Yii::$app->get('cdn');
//                $cdn->host = Yii::$app->feehi->website_url;
                /** @var User $model */
                return $model->getCurrentAvatar();
            },
            'age',
            'personal_cer' => function ($model) {
//                if (UserCertification::findOne(['uid'    => $model->id,
//                                                'status' => UserCertification::SUCCESS_STATUS,
//                                                'type'   => UserCertification::PERSONAL_TYPE
//                ])) {
//                    return 1;
//                } else {
//                    return 0;
//                }
                /** @var User $model */
                return $model->personCer;
            },
            'business_cer' => function ($model) {
//                if (UserCertification::findOne(['uid'    => $model->id,
//                                                'status' => UserCertification::SUCCESS_STATUS,
//                                                'type'   => UserCertification::BUSINESS_TYPE
//                ])) {
//                    return 1;
//                } else {
//                    return 0;
//                }
                /** @var User $model */
                return $model->businessCer;
            },
            'gender',
            'signature',
            'description',
            'real_name',
            'phone',
            'wx_avatar',
            'wx_name',
            'created_at'   => function ($model) {
                return date('Y-m-d H:i:s', $model->created_at);
            },
        ];
    }


    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['username', 'password', 'repassword'], 'string'],
//            [['avatar'], 'file', 'skipOnEmpty' => true, 'extensions' => 'png, jpg, jpeg, gif, webp'],
//            ['email', 'email'],
            [['repassword'], 'compare', 'compareAttribute' => 'password'],
            [['status'], 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
//            [['username', 'email', 'password', 'repassword'], 'required', 'on' => ['create']],
//            [['username', 'email'], 'required', 'on' => ['update']],
//            [['gender'], 'in', 'range' => [self::MAN, self::WOMAN]],
            [['status', 'created_at', 'updated_at', 'gender', 'age', "avatar"], 'integer'],
            [
                [
                    'username',
                    'password_hash',
                    'password_reset_token',
                    'email',
                    'signature',
                    'description',
                    'real_name',
                    'phone',
                    'access_token',
                    'wx_openid',
                    'wx_avatar',
                    'wx_name'
                ],
                'string',
                'max' => 255
            ],
            [['auth_key'], 'string', 'max' => 32],
            [['wx_openid'], 'unique'],
//            [['email'], 'unique'],
            [['password_reset_token', 'access_token'], 'unique'],


        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'username'             => Yii::t('app', 'Username'),
            'email'                => Yii::t('app', 'Email'),
            'old_password'         => Yii::t('app', 'Old Password'),
            'password'             => Yii::t('app', 'Password'),
            'repassword'           => Yii::t('app', 'Repeat Password'),
            'avatar'               => Yii::t('app', 'Avatar'),
            'status'               => Yii::t('app', 'Status'),
            'created_at'           => Yii::t('app', 'Created At'),
            'updated_at'           => Yii::t('app', 'Updated At'),
            'id'                   => '自增用户id',
            'auth_key'             => 'cookie验证auth_key',
            'password_hash'        => '加密后密码',
            'password_reset_token' => '找回密码token',
            'signature'            => '个性签名',
            'description'          => '个人简介',
            'gender'               => '性别',
            'real_name'            => '真实姓名',
            'phone'                => '联系电话',
            'age'                  => '年龄',
            'access_token'         => '存取令牌',
            'wx_openid'            => '微信openid',
            'wx_avatar'            => '微信头像',
            'wx_name'              => '微信昵称',
            'personCer'            => '个人认证',
            'businessCer'          => '商家认证',
        ];
    }


    public function getPersonalCer()
    {
        $this->userCertifications;

        return '';
    }


    /**
     * @inheritdoc
     */
    public function beforeSave($insert)
    {
        if ($insert) {
            $this->generateAuthKey();
            if ( ! isset($this->password)) {
                $this->password = "123456";
            }
            $this->setPassword($this->password);
            $this->email      = '';
            $this->created_at = time();
            $this->updated_at = $this->created_at;
        } else {
            $this->updated_at = time();

            if ( ! empty($this->password) && empty($this->repassword)) { //重制密码
                $this->addError("repassword",
                    Yii::t('yii', '{attribute} must be equal to "{compareValueOrAttribute}".', [
                        'attribute'               => yii::t('app', 'Repeat Password'),
                        'compareValueOrAttribute' => yii::t('app', 'Password')
                    ])
                );

                return false;
            }
            $this->setPassword($this->password);
        }

        return parent::beforeSave($insert);
    }


}